package com.bvbot.core.session;

import static com.bvbot.core.Logger.log;

import java.math.BigDecimal;

public class BuyResponse extends BvResponse {

	private Integer gramsBought;
	private Boolean errorResponse;

	public BuyResponse(String content) {
		super(content);
	}

	public String getSummary() {
		StringBuilder sb = new StringBuilder();
		sb.append("" + getGramsBought() + " grams have been bought.");
		return sb.toString();
	}

	public Integer getGramsBought() {
		if (gramsBought == null) {
			String qtyMatchedString = "quantityMatched";
			String qtyMatched = extractAttributValue(qtyMatchedString,
					getContent());
			if (qtyMatched != null) {
				BigDecimal tmp = getValueMultipliedBy1000(qtyMatched);
				if (tmp != null) {
					gramsBought = tmp.intValueExact();
				}
			}
		}
		return gramsBought;
	}

	public boolean isErrorResponse() {
		if (errorResponse == null) {
			errorResponse = false;
			String statusCodeString = "statusCode";
			String statusCodeValue = extractAttributValue(statusCodeString,
					getContent());
			if (statusCodeValue != null) {
				if (!"DONE".equals(statusCodeValue)) {
					log("The status code is declared in error : "
							+ statusCodeValue);
					errorResponse = true;
				}
			}
		}
		return errorResponse;
	}

}
